const express = require('express');
const router = express.Router();
const Supplier = require('../models/supplierModel');

/**
 * @swagger
 * /api/suppliers:
 *   get:
 *     summary: 获取所有供应商
 *     tags: [Supplier]
 *     responses:
 *       200:
 *         description: 返回供应商列表
 */
router.get('/', async (req, res) => {
    const data = await Supplier.getAll();
    res.json(data);
});

/**
 * @swagger
 * /api/suppliers/{id}:
 *   get:
 *     summary: 获取指定供应商信息
 *     tags: [Supplier]
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         description: 供应商ID
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 返回供应商信息
 *       404:
 *         description: 找不到供应商
 */
router.get('/:id', async (req, res) => {
    const data = await Supplier.getById(req.params.id);
    if (!data) {
        res.status(404).json({ message: 'Supplier not found' });
    } else {
        res.json(data);
    }
});

/**
 * @swagger
 * /api/suppliers:
 *   post:
 *     summary: 创建供应商
 *     tags: [Supplier]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *             properties:
 *               name:
 *                 type: string
 *               type:
 *                 type: string
 *               level:
 *                 type: integer
 *                 description: 1-5等级
 *               tax_number:
 *                 type: string
 *               legal_person:
 *                 type: string
 *               legal_phone:
 *                 type: string
 *               contact_person:
 *                 type: string
 *               contact_phone:
 *                 type: string
 *               bank_account:
 *                 type: string
 *               address:
 *                 type: string
 *               remarks:
 *                 type: string
 *               status:
 *                 type: integer
 *                 description: 1=正常，0=禁用，2=删除
 *     responses:
 *       201:
 *         description: 创建成功
 *       400:
 *         description: 请求数据不合法
 */
router.post('/', async (req, res) => {
    const data = await Supplier.create(req.body);
    res.status(201).json(data);
});

/**
 * @swagger
 * /api/suppliers/{id}:
 *   put:
 *     summary: 更新供应商信息
 *     tags: [Supplier]
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         description: 供应商ID
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               type:
 *                 type: string
 *               level:
 *                 type: integer
 *               tax_number:
 *                 type: string
 *               legal_person:
 *                 type: string
 *               legal_phone:
 *                 type: string
 *               contact_person:
 *                 type: string
 *               contact_phone:
 *                 type: string
 *               bank_account:
 *                 type: string
 *               address:
 *                 type: string
 *               remarks:
 *                 type: string
 *               status:
 *                 type: integer
 *     responses:
 *       200:
 *         description: 更新成功
 *       404:
 *         description: 找不到供应商
 */
router.put('/:id', async (req, res) => {
    const data = await Supplier.update(req.params.id, req.body);
    res.json(data);
});

/**
 * @swagger
 * /api/suppliers/{id}:
 *   delete:
 *     summary: 软删除供应商
 *     tags: [Supplier]
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         description: 供应商ID
 *         schema:
 *           type: string
 *     responses:
 *       204:
 *         description: 删除成功（软删除）
 *       404:
 *         description: 找不到供应商
 */
router.delete('/:id', async (req, res) => {
    await Supplier.delete(req.params.id);
    res.status(204).send();
});

module.exports = router;
